package view.project;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import dal.project.Project;
import dal.project.ProjectUtil;

/**
 * Servlet implementation class AddProject
 */
@WebServlet("/AddProject")
public class AddProject extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProject() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
	  /*+------------+-------------+------+-----+---------+-------+
		| Field      | Type        | Null | Key | Default | Extra |
		+------------+-------------+------+-----+---------+-------+
		| Project_id | varchar(30) | NO   | PRI |         |       |
		| Date       | date        | YES  |     | NULL    |       |
		| Title      | tinytext    | YES  |     | NULL    |       |
		| Owner      | tinytext    | YES  |     | NULL    |       |
		| Location   | tinytext    | YES  |     | NULL    |       |
		+------------+-------------+------+-----+---------+-------+
		*/
		
		PrintWriter printWriter=response.getWriter();
		response.setContentType("text/HTML");
		
		response.setHeader("Cache-control", "no-cache, no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");
		
		response.setHeader("Access-Control-Allow-Origins", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
	    response.setHeader("Access-Control-Max-Age", "86400");
		
	    Gson gson=new Gson();
		JsonObject myJsonObj=new JsonObject();
		String statusText="";
	    
		try {
			
			java.util.Date d=new java.util.Date();
			DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			String date=df.format(d);
			
			Date sqlDate=new Date(d.getTime());
			
			String title=request.getParameter("projectTitle");
			String owner=request.getParameter("projectOwner");
			String location=request.getParameter("projectLocation");
			
			Project project=new Project();
			ProjectUtil projectUtil=new ProjectUtil();
			
			String id=projectUtil.getId();
			
			project.setProjectId(id);
			project.setDate(sqlDate);
			project.setTitle(title);
			project.setOwner(owner);
			project.setLocation(location);
			
			
			boolean isOk=projectUtil.add(project);
			if(isOk){
				
				myJsonObj.addProperty("success", true);
				
				statusText="<b>"
						+ " Project Information Added Succesfully "
						+ "</b>";
			}else {
				
				myJsonObj.addProperty("success", false);
				
				statusText="<b>"
						+ " Sorry ! Unable to add Project Information "
						+ "</b>";
			}
			
		} catch (Exception e) {
			myJsonObj.addProperty("success", false);
			statusText="<b>"
					+ " Sorry ! Error occured while processing request "
					+ "</b>";
		}
		
		myJsonObj.addProperty("statusText", statusText);
		printWriter.println(myJsonObj.toString());
		printWriter.close();
		
	}

}
